package com.snail.readyvr.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.snail.readyvr.R;
import com.snail.readyvr.adapter.CommonAdapter;
import com.snail.readyvr.adapter.ViewHolder;
import com.snail.readyvr.ui.CommonTopBar;
import java.util.Arrays;
import java.util.List;

/**
 * Created by MRKING on 2016/11/21.
 */

public class SelectHeadIconDialog {


    public static class Builder {
        private Context mContext;
        private View mContentView;
        private CommonAdapter<Integer> mAdapter;
        private List<Integer> mItems;
        private DialogInterface.OnClickListener mOnClickListener;
        private GridView mGridView;
        private String mTitle;

        public Builder(Context context) {
            mContext = context;
        }


        public Builder addSingleChoiceItems(Integer[] items, String title, DialogInterface.OnClickListener listener) {
            mItems = Arrays.asList(items);
            mOnClickListener = listener;
            mTitle = title;
            return this;
        }

        public AlertDialog show() {

            mContentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_head_icon_layout, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setCancelable(true);
            final AlertDialog dialog = builder
                    .setView(mContentView)
                    .show();
            dialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent);
            dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
            mGridView = (GridView) mContentView.findViewById(R.id.dialog_head_icon_list);
            CommonTopBar commonTopBar = (CommonTopBar) mContentView.findViewById(R.id.dialog_top_bar);
            commonTopBar.midTextView.setText(mTitle);
            commonTopBar.setOnClickCommonTopBar(new CommonTopBar.OnClickCommonTopBar() {
                @Override
                public void onClickLeftView() {
                    dialog.dismiss();
                }

                @Override
                public void onClickRightView() {

                }
            });
            mAdapter = new CommonAdapter<Integer>(mContext, R.layout.dialog_head_icon_item) {
                @Override
                public void convert(ViewHolder helper, Integer item, final int position) {

                    if (helper == null || item == null)
                        return;
                    helper.setBackGroundResource(R.id.dialog_head_icon_img, item);

                }
            };
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (mOnClickListener != null)
                        mOnClickListener.onClick(dialog, position);
                }
            });
            mGridView.setAdapter(mAdapter);
            mAdapter.addData(mItems, true, false);
            return dialog;
        }
    }

}

